<?php
// The filemanger callback only works when you load it through DOM do not use X-Javascript.

class FileManager extends FormElement
{
	static private $bLoadJavaScript = true;
	
	public function __construct($name, $label = null, $value = null, $callback = null)
	{
		parent::__construct($name, array
		(
			'name' => $name,
			'label' => $label,
			'value' => $value,
			'callback' => $callback
		));
		
		HeaderManager::addHeader(HeaderManager::HEADER_JAVASCRIPT, 'Lib/FileManager/public/js/startup.js');
	}

	public function __toString() {

		if (self::$bLoadJavaScript)
		{
			self::$bLoadJavaScript = false;
			
			HeaderManager::addHeader(HeaderManager::X_JAVASCRIPT, 'Lib/FileManager/public/js/form_element.js');
		}
		
		$html = '<div class="form filemanager"><input type="hidden" name="'.$this->getProperty('name').'" id="'.$this->getProperty('name').'" class="fileManagerInputField" value="'.$this->getProperty('value').'" />';
		
		if ($this->getProperty('label') !== null)
		{
			$html .= '<a href="#'.$this->getProperty('callback').'" rel="'.$this->getProperty('name').'" class="fileManagerPopup" onclick="OpenFileManagerFormElement(this); return false;">'.$this->getProperty('label').'</a>';
		}
		
		$html .= '</div>';

		return $html;

		//return '<div class="form filemanger"><label for="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '">' . $this->getProperty('label') . '</label><a href="#'.$this->callback.'" id="openPopup">Selecteer een afbeelding.</a>' . ( $this->inAJAXMode() || !empty( $this->errorMessage ) ? '<div class="error' . ( $this->inAJAXMode() ? ' ajax' : '' ) . '" id="' . ( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name . '_error">' . $this->errorMessage . '</div>' : '' ) . '</div>';
	}
}
?>